package com.xxgc.wujiaoservice.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxgc.wujiaoservice.entity.UserFriendEntity;
import com.xxgc.wujiaoservice.entity.UserFriendEntityRepository;
import com.xxgc.wujiaoservice.entity.UsersEntity;
import com.xxgc.wujiaoservice.entity.UsersEntityRepository;
import com.xxgc.wujiaoservice.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 获取用户信息
 */
@RestController
@RequestMapping("/users")
@Tag(name = "用户信息")
public class UserInfoController {

    @Autowired
    private UsersEntityRepository usersEntityRepository;
    @Autowired
    private UserFriendEntityRepository userFriendEntityRepository;

    @PostMapping("/addFriend")
    public Result addFriend(@RequestBody Map<String,Object> map){
        //获取用户的id
        String loginId = StpUtil.getLoginIdAsString();
        System.out.println("loginIdAsInt = " + loginId);
        String nickName = (String) map.get("nickName");
        System.out.println("nickName = " + nickName);

        //通过昵称查询对方id
        QueryWrapper<UsersEntity> qw = new QueryWrapper<>();
        qw.eq("nickname",nickName);
        UsersEntity user = usersEntityRepository.getOne(qw);
        //判断
        if(user == null){
            return Result.fail("没有这个用户");
        }
        //好友申请
        UserFriendEntity userFriendEntity = new UserFriendEntity();
        userFriendEntity.setUserId(loginId);//自己的id
        userFriendEntity.setFriendId(user.getId());//对方对id
        userFriendEntity.setState(0);//状态 0 待添加
        userFriendEntityRepository.save(userFriendEntity);
        return Result.success();
    }


    //getFriendList 获取等待添加对好友列表
    @GetMapping("/getFriendList")
    public Result getFriendList(){
        String loginId = StpUtil.getLoginIdAsString();
        QueryWrapper<UserFriendEntity> qw = new QueryWrapper<>();
        //查询谁在添加我
        qw.eq("state",0);
        qw.eq("friend_id",loginId);
        List<UserFriendEntity> list = userFriendEntityRepository.list(qw);
        //查询到谁在添加我，把对方对信息查询出来打包成一个list返回给页面
        ArrayList<UsersEntity> userList = new ArrayList<>();
        for (UserFriendEntity userFriendEntity : list) {
            userList.add(usersEntityRepository.getById(userFriendEntity.getUserId()));
        }
        return Result.success(userList);
    }


    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        //获取用户的id
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        System.out.println("loginIdAsInt = " + loginIdAsInt);
        UsersEntity usersEntity = usersEntityRepository.getById(loginIdAsInt);
        return Result.success(usersEntity);
    }


    // 修改性别
    @Operation(summary = "修改性别")
    @PutMapping("/updateSex")
    public Result updateSex(@RequestBody Map map){
        //token
        String id = StpUtil.getLoginIdAsString();
        String sex = (String) map.get("sex");
        //new
        UsersEntity usersEntity = new UsersEntity();
        usersEntity.setId(id);
        usersEntity.setSex(sex);
        usersEntityRepository.updateById(usersEntity);
        return Result.success();
    }



}
